package net.oschina.app.base;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import net.oschina.app.interf.IUIOperation;

public abstract class BaseFragment extends Fragment implements IUIOperation {
	
	/** 管理Fragment的Activity */
	public BaseActivity mActivity;

	@Override
	public void onAttach(Context context) {
		super.onAttach(context);
		mActivity = (BaseActivity)getActivity();
	}

	/** Fragment显示的布局 */
	public View mRoot;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		if (mRoot == null) {
			mRoot = View.inflate(getActivity(),getLayoutRes(),null);
			
			initView();
			initListener();
			initData();
			
		} else {
			// 解除mRoot与其父控件的关系
			unbindWidthParent(mRoot);
		}
		
		return mRoot;
	}

	/**
	 * 解除父子控件关系                                                     
	 * 
	 * @param view
	 */
	public void unbindWidthParent(View view) {
		ViewGroup parent = (ViewGroup) view.getParent();
		if (parent != null) {
			parent.removeView(view);
		}
	}
	
	public void showToast(String text) {
		Global.showToast(text);
	}
}

